<?php
/**
 * 分页类
 * @author zengnz
 * 提供多种不同的分页格式
 */
class Page
{
	//总记录数
	protected $totalCount;
	//当前页
	protected $nowPage;
	//总页数
	protected $totalPages;
	//页面跳转时传递的参数
	protected $parameter;
	
	//起始行
	public $firstRow;
	//每页显示记录数
	public $listRows = 6;
	
	public function __construct( $totalCount, $nowPage, $listRows = "", $parameter = "" ) {
		$this -> totalCount = intval( $totalCount );
		if ( $listRows )
			$this -> listRows = intval( $listRows );
		
		$this -> nowPage = $nowPage;
		$this -> firstRow = $this -> listRows * ( $this -> nowPage -1 );
		$this -> parameter = $parameter;
		$this -> totalPages = ceil( $this -> totalCount / $this -> listRows );
	}
	
	/**
	 * ajax分页
	 * @param integer $listPages 显示的页数
	 * 格式 首页 上一页  2 3 4 5 6 下一页  末页
	 */
	public function ajaxList( $listPages = 5 ) {
		$pageList = '';
		$startPage = 1;
		$endPage = $this -> totalPages;
		
		$prev = ( $this -> nowPage - 1 ) ? $this -> nowPage - 1 : 1;
		$next = ( $this -> nowPage + 1 <= $this -> totalPages ) ? ( $this -> nowPage + 1 ) : $this -> totalPages; 
		
		if ( $this -> totalPages <= 1)
			return $pageList;
		
		if ( $this -> nowPage > 1 ) 
			$pageList .= '<span class="page" page="1">首页</span><span class="page" page='. $prev .'>上一页</span>';
		else
			$pageList .= '<span class="nonePage">首页</span><span class="nonePage">上一页</span>';
		
		if ( $listPages < $this -> totalPages ) {
			$middleNum = floor( $listPages / 2 );
			
			if ( $this -> nowPage <= $middleNum + 1 ) {
				$startPage = 1;
				$endPage = $listPages;
			} else {
				$endPage = $this -> nowPage + $middleNum;
				$endPage = ( $endPage <= $this -> totalPages ) ? $endPage : $this -> totalPages;
				$startPage = $endPage - $listPages + 1;
				$startPage = ( $startPage > 0 ) ? $startPage : 1;
			}
		}
		
		for ( $i = $startPage; $i <= $endPage; $i++ ) {
			if ( $i == $this -> nowPage )
				$pageList .= '<span class="currentPage">' . $i . '</span>';
			else
				$pageList .= '<span class="page" page=' . $i . '>' . $i . '</span>';
		}
		
		if ( $this -> nowPage < $this -> totalPages )
			$pageList .= '<span class="page" page='. $next .'>下一页</span><span class="page" page='.$this -> totalPages.'>末页</span>';
		else 
			$pageList .= '<span class="nonePage">下一页</span><span class="nonePage">末页</span>';
		
		return $pageList;
	}
	
	/**
	 * 常规分页 
	 * @param integer $listPages 显示的页数
	 * 格式 首页 上一页  2 3 4 5 6 下一页  末页 跳转至 _页
	 */
	public function commonList( $listPages = 5 ) {
		$url = $_SERVER["PHP_SELF"] . "?" . $this -> parameter;
		
		$pageList = '';
		$startPage = 1;
		$endPage = $this -> totalPages;
		$prev = ( $this -> nowPage - 1 ) ? $this -> nowPage - 1 : 1;
		$next = ( $this -> nowPage + 1 <= $this -> totalPages ) ? ( $this -> nowPage + 1 ) : $this -> totalPages;
		
		if ( $this -> totalPages <= 1)
			return $pageList;
		
		if ( $this -> nowPage > 1 ) {
			$firstPageUrl = $url . '&p=1';
			$prevPageUrl = $url . '&p=' . $prev;
			$pageList .= '<a class="firstPage" href="'. $firstPageUrl .'" target="_self">首页</a><a class="prevPage" href="'. $prevPageUrl .'" target="_self">上一页</a>';
		}
		
		if ( $listPages < $this -> totalPages ) {
			$middleNum = floor( $listPages / 2 );
				
			if ( $this -> nowPage <= $middleNum + 1 ) {
				$startPage = 1;
				$endPage = $listPages;
			} else {
				$endPage = $this -> nowPage + $middleNum;
				$endPage = ( $endPage <= $this -> totalPages ) ? $endPage : $this -> totalPages;
				$startPage = $endPage - $listPages + 1;
				$startPage = ( $startPage > 0 ) ? $startPage : 1;
			}
		}
		
		for ( $i = $startPage; $i <= $endPage; $i++ ) {
			if ( $i == $this -> nowPage )
				$style = "currentPage";
			else 
				$style = "page";
			
			$pageUrl = $url . '&p=' . $i;
			$pageList .= '<a class="'. $style .'" href="'. $pageUrl .'" target="_self">'. $i .'</a>';
		}
			
		if ( $this -> nowPage < $this -> totalPages ) {
			$nextPageUrl = $url . '&p=' . $next;
			$endPageUrl = $url . '&p=' . $this -> totalPages;
			$pageList .= '<a class="nextPage" href="'. $nextPageUrl .'" target="_self">下一页</a><a class="endPage" href="'. $endPageUrl .'" target="_self">末页</a>';
		}
		
		return $pageList;
	}
	
	/**
	 * 间隔分页
	 * 格式 上一页 2 3 4...10 11 12...20 21 22 下一页
	 */
	public function spacingList() {
		
		
	}
	
	
}